use std::{fs, io::Write};

fn download(name: &str) {
    let url = format!("https://blognashzhou.oss-cn-shanghai.aliyuncs.com/{}.json", name);
    let current_dir = std::env::current_dir().unwrap();
    let dest_path = std::path::Path::new(&current_dir).join(format!("{}.json", name));

    if !dest_path.exists() {
        let response = reqwest::blocking::get(url).unwrap();
        if response.status().is_success() {
            let mut file = fs::File::create(dest_path).unwrap();
            file.write_all(&response.bytes().unwrap()).unwrap();
        }
        // println!("cargo:rerun-if-changed=build.rs"); // 当 build.rs 改变时重新运行
    }
    // 告诉 Rust 编译器数据文件的位置
    // println!("cargo:rerise-if-env-changed=DATA_FILE_PATH_{}", name);
    // println!("cargo:rustc-env=DATA_FILE_PATH_{}={}", name, dest_path.display());
}

fn main() {
    download("s2t");
    download("t2s");
    download("STPhrases");
    download("TSPhrases");
    download("pinyin");
}